﻿#ifndef _CROSS_COM_H
#define _CROSS_COM_H
#ifdef   _WIN32
#elif defined __linux
#else
	#error    UNKNOWN ENVIRONMENT
#endif
#include <stdint.h>
#include <assert.h>
#include <iostream>
#include <fstream>
#include <string.h>
#include <list>
using namespace std;

#ifdef _WIN32
#include "stdafx.h"
#include <io.h>
//#include "afxsock.h"
#include <windows.h>
#include "winbase.h"
#include <vfw.h>

typedef  DWORD   RESULT;
typedef  __int64  LONGLONG;
typedef  FILETIME  DVRDATETIME;

#else //////Linux����
#include <sys/syscall.h>
#include <pthread.h>
#include <time.h>
#include <string.h>
#include <sys/time.h>
#include <pthread.h>
#include <unistd.h>
#include <semaphore.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <signal.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#define  WINAPI        *
#define  printErrorLocation(m)		{perror( m ); printf("%s():%s:%d\n", __FUNCTION__, __FILE__, __LINE__); }

typedef  timeval       DVRDATETIME;
typedef  void          RESULT;
typedef  void*         LPVOID;
typedef  unsigned long int HANDLE;
typedef  long long     LONGLONG;
typedef  char       BYTE;
typedef  unsigned int DWORD;
typedef  RESULT WINAPI (*LPTHREAD_START_ROUTINE)(LPVOID lpParameter);
#endif


#ifdef   _WIN32
#include <winbase.h>
typedef  HANDLE						Cross_thread_t;			//�߳�ID
typedef  LPTHREAD_START_ROUTINE		Cross_start_routine;	//�̺߳���
typedef  CRITICAL_SECTION			Cross_lock_t;			//��
typedef	 HANDLE						Cross_sem_t;			//�ź���
typedef	 DWORD						Cross_THREAD_RESULT;	//�̺߳���ؽ��
#define	 PUB_THREAD_CALL			WINAPI					//������÷�ʽ
#define  PUB_CREATE_THREAD_FAIL		NULL					//�����߳�ʧ��
#define  snprintf                   _snprintf
#define  TRACE                      printf
#elif defined __linux
typedef	unsigned long long			ULONGLONG;
typedef  pthread_t					Cross_thread_t;			//�߳�ID
typedef  void *(*start_routine)(void*);
typedef  start_routine				Cross_start_routine;		//�̺߳���
typedef  pthread_mutex_t			Cross_lock_t;				//��
typedef	 sem_t						Cross_sem_t;				//�ź���
typedef	 void*						Cross_THREAD_RESULT;		//�̺߳���ؽ��
#define	 PUB_THREAD_CALL									//������÷�ʽ
#define  PUB_CREATE_THREAD_FAIL		0						//�����߳�ʧ��
#endif

typedef  int (*MESSAGE_CALLBACK)(void* pObject, int iMsg, void *pParam1, void *pParam2);
typedef  int (*MESSAGE_CALLBACK_EX)(void* pObject, int module, int iMsg, void *pParam1, void *pParam2);

#define PUB_THREAD_ID_NOINIT  PUB_CREATE_THREAD_FAIL    //û�г�ʼ�����߳�ID

// _PUB_DEBUG_ERROR		_PUB_DEBUG_WARN		_PUB_DEBUG_INFO Ϊ������Լ���
#ifdef	__linux
#if		defined _PUB_DEBUG_ERROR || defined _PUB_DEBUG_WARN || defined _PUB_DEBUG_INFO
	#include  <stdio.h>
	#define   PUB_PRINTF_ERROR(format,argc...) do{printf(format, ##argc);exit(0);}while(0);
	#if		defined _PUB_DEBUG_WARN || defined _PUB_DEBUG_INFO
		#define	  PUB_PRINTF_WARN(format,argc...) printf(format, ##argc)
		#if defined _PUB_DEBUG_INFO
			#define	  PUB_PRINTF_INFO(format,argc...) printf(format, ##argc)
		#else
			#define   PUB_PRINTF_INFO(format,argc...)
		#endif
	#else
		#define   PUB_PRINTF_INFO(format,argc...)
		#define   PUB_PRINTF_WARN(format,argc...)
	#endif
#else
	#define   PUB_PRINTF_INFO(format,argc...)
	#define   PUB_PRINTF_WARN(format,argc...)
	#define   PUB_PRINTF_ERROR(format,argc...)
#endif
#elif defined _WIN32
#if		defined _PUB_DEBUG_ERROR || defined _PUB_DEBUG_WARN || defined _PUB_DEBUG_INFO
	#include  <stdio.h>
	#define   PUB_PRINTF_ERROR TRACE
	#if		defined _PUB_DEBUG_WARN || defined _PUB_DEBUG_INFO
		#define	  PUB_PRINTF_WARN TRACE
		#if defined _PUB_DEBUG_INFO
			#define	  PUB_PRINTF_INFO TRACE
		#else
			#define   PUB_PRINTF_INFO
		#endif
	#else
		#define   PUB_PRINTF_INFO
		#define   PUB_PRINTF_WARN
	#endif
#else
	#define   PUB_PRINTF_INFO
	#define   PUB_PRINTF_WARN
	#define   PUB_PRINTF_ERROR
#endif
#endif

HANDLE ComCreateThread(unsigned long int* threadid, void* arg, LPTHREAD_START_ROUTINE  start_routine);
void ComExitThread(HANDLE ThreadHandle);

#define  HAS_TIME_ZONE 1

////sleep�Ժ���Ϊ��λ
//void DVRSleep(int ms);
inline void DVRSleep(int ms)//����
{
#ifdef _WIN32
	Sleep(ms);
#else
	usleep(ms*1000);
#endif
}

unsigned long GetBuildDate();

inline void FileFlush(FILE *pFile)
{
	fflush(pFile);
#ifdef _WIN32
//	_commit(_fileno(pFile));
#else
	fsync(fileno(pFile));
#endif
}


LONGLONG TimeToDVRTime(DVRDATETIME  DVRtm);
unsigned long TimeToDVRTime32(DVRDATETIME  DVRtm);

tm DVRTimeToTm(LONGLONG DVRtm);
tm DVRTime32ToTm(unsigned long DVRtm);

LONGLONG TmToDVRTime(tm time);
unsigned long TmToDVRTime32(tm time);

LONGLONG GetCurrTime();
unsigned long GetCurrTime32();



void Release (FILE **pFile, unsigned long num, ...);

BYTE GetChnnForbits(ULONGLONG chnnbits);

bool IsValidIPCfg(DWORD dwIP,DWORD dwSubnetMask,DWORD dwGateWay);

class CTimeInterval
{
public:
	CTimeInterval();
	virtual ~CTimeInterval();
	void	Start();
	int		GetCurInterval();
	int		Restart();
private:
	LONGLONG m_starttime;

	double m_dfFreq;  //

	LONGLONG m_QPart1,m_QPart2;

};

/**********************************************************************************
*
*                          class CWaitEvent
*
**********************************************************************************/

#define INFINITE            0xFFFFFFFF// Infinite timeout

class CWaitEvent
{
public:
	CWaitEvent();
	~CWaitEvent();
	void CreateWaitEvent();
	unsigned long WaitForCond(DWORD dwMilliseconds = INFINITE);  /////�ȴ��ź� Ĭ�ϲ���Ϊ
	void SetEvent();  /////����Ϊ���ź�
	void PulseEvent();
	void Close();
private:

#ifdef _WIN32
	HANDLE           m_Event;
#else
	void Gettimespec(struct timespec* ptimespec, int offset);
	pthread_mutex_t  m_lock;
	pthread_cond_t   m_Event;
#endif
};

//�����߳�
//Cross_thread_t Cross_CreateThread(Cross_start_routine start_routine, void* pParam, bool *pRun);
Cross_thread_t Cross_CreateThreadEx(Cross_start_routine start_routine, void* pParam, bool *pRun, int policy);
//����̣߳�������ȴ��߳̽���
void Cross_ExitThread(Cross_thread_t *pThreadID, bool *pRun);


void Cross_PrintError(const char* pFile, int iLine);


//void CrossSleep(unsigned int dwMillisecond);


//void Cross_InitLock(Cross_lock_t *pLock);

void Cross_DestroyLock(Cross_lock_t *pLock);

#if 0
class CCross_Lock
{
public:
	CCross_Lock();
	~CCross_Lock();

	void Lock();	//����
	void UnLock();	//����
#ifdef	__linux
	void CondWait();
	void CondSignal();
	int TimeLock(int iMicrosecond);	//BUGS��arm-uclibc-linux-g++�ϱ��룬ִ��������������⣬���ܳ�ʱ
	int TryLock();
#endif
private:
	Cross_lock_t m_Lock;
#ifdef	__linux
	pthread_cond_t m_cond;
#endif
};
#endif

class CCross_Sem
{
public:
	CCross_Sem();
	~CCross_Sem();

	void Wait();
	void Post();

private:
	Cross_sem_t m_Sem;
};


class CLock
{

public:
	CLock();
	void Lock();
	bool TryLock();	
	void Unlock();
	virtual ~CLock();
protected:
private:
#ifndef _WIN32
	pthread_mutex_t    m_MutexLock;
	pthread_mutexattr_t m_MutexAttr;// = PTHREAD_MUTEX_RECURSIVE_NP;
#else
	CRITICAL_SECTION   m_MutexLock;
#endif

};


bool Cross_HasBit64(const ULONGLONG& ullNum, int iIndex);


bool Cross_IsEmptyBit64(const ULONGLONG& ullNum);


void Cross_SetBit64(ULONGLONG *pllNum, int iIndex);

void Cross_ClearBit64(ULONGLONG *pllNum, int iIndex);


bool Cross_HasBit32(unsigned long ulNum, int iIndex);

void Cross_SetBit32(unsigned long *pNum, int iIndex);


bool GetKernelverionEx(unsigned long *pVersion);


bool FindStringInFile(const char *pFilePath,const char *pFindContent);

void GetURLAndPort(char *inputUrl,char *outputUrl,char *outputUrlEnd,unsigned short &port);
#ifdef _WIN32
int gettimeofday(struct timeval *tp, void *tzp);
#endif // _WIN32
int GetDvrCurTime(int64_t* pDvrTime,int64_t diffMS = 0);
void PrintTimeMs(int64_t tmStart ,int64_t tmEnd = 0, const char * strTitle=NULL);
void PrintTimeSec(time_t tmStart ,time_t tmEnd, const char * strTitle);
int ShowHexData( char *pbuf, int dataLen, int breakLine, bool have_blank, char *str_title);
#endif //_CROSS_COM_H
